#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXSIZE 100
#define ERROR -1

typedef int Position;
typedef struct LNode *PtrToLNode;
struct LNode {
    double Data[MAXSIZE];
    Position Last;
};
typedef PtrToLNode List;

List MakeEmpty();
Position Find(List L, double X);
bool Insert(List L, double X, int i);
bool Delete(List L, int i);

int main()
{
    List example_list;
    double number;

    example_list = MakeEmpty();
    Position insert_position = 1, delete_position = 0;

    for (;;)
    {
        printf("Enter the number to input(0 to finish) : ");
        scanf("%lf", &number);
        if (number == 0)
            break;
        if (Insert(example_list, number, insert_position++))
            printf("Insert Success !\n");
        else
            break;
    }

    printf("\nThe list have %d numbers : \n", example_list->Last + 1);

    for (int i = 0; i < example_list->Last + 1; i++)
        printf("%lf  ", example_list->Data[i]);
    printf("\n\n");

    for (;;)
    {
        printf("Enter the number position to delete(0 to finish) : ");
        scanf("%d", &delete_position);
        if (delete_position == 0)
            break;
        if (Delete(example_list, delete_position))
            printf("Delete Success !\n");
        else
            break;
    }

    printf("\nThe list have %d numbers : \n", example_list->Last + 1);

    for (int i = 0; i < example_list->Last + 1; i++)
        printf("%lf  ", example_list->Data[i]);    
    printf("\n");

    return 0;
}

List MakeEmpty()
{
    List L;
    L = (List)malloc(sizeof(struct LNode));
    L->Last = -1;
    return L;
}

Position Find(List L, double X)
{
    Position i = 0;
    while (i <= L->Last && L->Data[i] != X)
        i++;
    if (i > L->Last)
        return ERROR;
    else
        return i;
}

bool Insert(List L, double X, int i)
{
    Position j;
    
    if (L->Last == MAXSIZE - 1)
    {
        printf("List is full !\n");
        return false;
    }
    
    if (i < 1 || i > L->Last + 2)
    {
        printf("Invalid Insert Position\n");
        return false;
    }
    
    for (j = L->Last; j >= i - 1; j--)
        L->Data[j + 1] = L->Data[j];

    L->Data[i - 1] = X;
    L->Last++;
    return true;
}

bool Delete(List L, int i)
{
    Position j;

    if (i < 1 || i > L->Last + 1)
    {
        printf("Element does not exist !");
        return false;
    }
    
    for (j = i; j <= L->Last; j++)
        L->Data[j - 1] = L->Data[j];

    L->Last--;
    return true;
}